<?php
/***************************************************************
*  Copyright notice
*  (c) 2009, jianyuzhu@gmail.com
*  All rights reserved
*  This script is part of the PPEMI project.
***************************************************************/

//
include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'frontend.main.class.php');
$feObj = new FrontendMainBase();

//
include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'module/content/content.category.class.php');
$mcObj = new ContentCategoryBase();
$mcObj->format = true;

//
include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'module/content/content.class.php');
$mObj = new ContentBase();
$mObj->format = true;

//
include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'module/menu/menu.tree.class.php');
$mtObj = new MenuTreeBase();
$mtObj->format = true;

//
if( $request_link_class == '' ) {
	$mObj->setQueryLimitLength(20);
	$items = $mObj->getItems('', 0);
	
	$feObj->content_wrap['items'] = $items;
} else {
	$item = $mObj->getItem($request_link_class, 'link');
	
	$feObj->addContent('mainpart', 'item');
	$feObj->addContent('item', $item);
	
	if( $item['id'] > 0 ) {
		//
		//1. item from category
		//	1a. item from menu
		//	1b. item from article
		//2. item from sub category
		//3. item no category
		//
		if( $item['cid'] > 0 ) {
			//
			$category = $mcObj->getCategory($item['cid'], 'id');
			$feObj->addContent('category', $category);
			
			//
			$categories = $mcObj->getCategoriesPath($item['cid']);
			$feObj->addContent('categories', $categories);
			
			//
			if( sizeof($categories) > 0 ) {
				$category_top = $categories['0'];
			} else {
				$category_top = $category;
			}
			
			/*
			//
			if( $item['app1'] == 1 && $item['app2'] > 0 ) {
				$item_menu = $mtObj->getItem($item['app2'], 'id');
				$item_root = $mtObj->getItemRoot($item_menu['id']);
				
				$items = $mtObj->getItemsTree('', $item_root['id']);
			} elseif( $item['app3'] == 1 && $item['app4'] > 0 ) {
				//
			} elseif( $category['categories_parent'] > 0 ) {
				$items = $mObj->getItems(array('cid' => $category['categories_parent'], 'app2:>' => '0'), 0, false);
			} elseif( $category['categories_id'] > 0 ) {
				$items = $mObj->getItems(array('cid' => $category['categories_id'], 'app2:>' => '0', 'app3:=' => '0'), 0, false);
			}
			*/
			$rt_cc = $tm_cc = $lc_cc = 0;
			if( substr($category_top['categories_type'], 0, 5) == 'menu:' ) {
				$mid = substr($category_top['categories_type'], 5);
				$items = $mtObj->getItemsTree('', $mid);
				$rt_cc = $mid;
				$tm_cc = $mid;
			} else {
				$items = $mObj->getItems(array('cid' => $category['categories_id']), 0, true);
			}
			if( substr($category['categories_type'], 0, 8) == 'content:' ) {
				$lc_cc = substr($category['categories_type'], 8);
			}
			
			$feObj->addContent('lc_items', $items);
			$feObj->addContent('tm_cc', $tm_cc);
			$feObj->addContent('lc_cc', $lc_cc);
			$feObj->addTitle($item['subject'], ' - ');
			
			//
			if( $item['app3'] == 1 ) {
				$feObj->addContent('category_show', 0);
			} elseif( $rt_cc > 0 ) {
				$feObj->addContent('category_show', 2);
			} elseif( $item['app3'] == $rt_cc ) {
				$feObj->addContent('category_show', 1);
			}
		} elseif( $item['app2'] > 0 ) {
			$item_menu = $mtObj->getItem($item['app2'], 'id');
			$item_root = $mtObj->getItemRoot($item_menu['id']);
			$items = $mtObj->getItemsTree('', $item_root['id']);
			
			$feObj->addContent('lc_items', $items);
		}
	} else {
		$feObj->redirect('error/nopage');	
	}
}

//
//echo $feObj->display('pagelayout/pagelayout_main_content.tpl');
//echo $feObj->display('pagelayout/pagelayout_content_page.tpl');
if( isset($_GET['edit']) ) {
	include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'user.session.class.php');
	$usObj = new UserSessionBase();
	$check = $usObj->checkAdmin($_REQUEST['PPSIDA']);
	if( $check == false ) {
		echo 'error';
		exit();
	}
	$content = $feObj->display('pagelayout/pagelayout_content_page_edit.tpl');
	//
	echo preg_replace("/<a href=\"([^\"]*)\"/", "<a href=\"$1?edit\"", $content);
} elseif( isset($_GET['manage']) ) {
	include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'user.session.class.php');
	$usObj = new UserSessionBase();
	$check = $usObj->checkAdmin($_REQUEST['PPSIDA']);
	if( $check == false ) {
		echo 'error';
		exit();
	}
	$content = $feObj->display('pagelayout/pagelayout_content_page_manage.tpl');
	//
	echo preg_replace("/<a href=\"([^\"]*)\"/", "<a href=\"$1?manage\"", $content);
} else {
	echo $feObj->display('pagelayout/pagelayout_content_page.tpl');
}
//
?>